﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DockSample.Line
{


    class ComplexLine : OriginalLine
    {

        public StartLine startLine;
        public EndLine endLine;
        public List<SimpleLine> simpleLineList = new List<SimpleLine>();
        public bool isComplex;
        OriginalLine selectedPart;

        public ComplexLine(Point startPoint, Point endPoint, OriginalDiagram startDiagram, OriginalDiagram endDiagram)
            : base(startPoint, endPoint, startDiagram, endDiagram)
        {
            this.isComplex = false;
            //             startDiagramSize = new Size(startDiagram.Width / 2, startDiagram.Height / 2);
            //             endDiagramSize = new Size(endDiagram.Width / 2, endDiagram.Height / 2);
        }
        public ComplexLine(Point startPoint, Point endPoint, string id, OriginalDiagram startDiagram, OriginalDiagram endDiagram)
            : base(startPoint, endPoint, id, startDiagram, endDiagram)
        {

            this.isComplex = false;
            //             startDiagramSize = new Size(startDiagram.Width / 2, startDiagram.Height / 2);
            //             endDiagramSize = new Size(endDiagram.Width / 2, endDiagram.Height / 2);
        }


        public ComplexLine(Point startPoint, OriginalDiagram startDiagram) : base(startPoint, startDiagram) { }
        public ComplexLine(Point startPoint, OriginalDiagram startDiagram, string id)
            : base(startPoint, startDiagram, id) { }

        public override void Drawing(Graphics g, DummyModelForm m)
        {
            if (!isComplex)
            {
                Pen pen = new Pen(Color.Brown, 1);
                Point start = GetStartShowPoint();
                Point end = GetEndShowPoint();
                //画箭头
                double flRlt = Math.Sqrt((start.X - end.X) * (start.X - end.X) + (start.Y - end.Y) * (start.Y - end.Y));
                float flength = 10;//箭头长度
                if (flRlt == 0) return;
                double tmpX = end.X + (start.X - end.X) * flength / flRlt;
                double tmpY = end.Y + (start.Y - end.Y) * flength / flRlt;
                double fl1X = (tmpX - end.X) * Math.Sqrt((float)3) / 2 + (tmpY - end.Y) * 1 / 2 + end.X;
                double fl1Y = (tmpY - end.Y) * Math.Sqrt((float)3) / 2 - (tmpX - end.X) * 1 / 2 + end.Y;
                double fl2X = (tmpX - end.X) * Math.Sqrt((float)3) / 2 - (tmpY - end.Y) * 1 / 2 + end.X;
                double fl2Y = (tmpY - end.Y) * Math.Sqrt((float)3) / 2 + (tmpX - end.X) * 1 / 2 + end.Y;
                g.DrawLine(pen, new Point(end.X, end.Y), new Point((int)fl1X, (int)fl1Y));
                g.DrawLine(pen, new Point(end.X, end.Y), new Point((int)fl2X, (int)fl2Y));
                //画线
                base.Drawing(g, m);
            }
            else
            {
                if (this.startLine != null)
                {
                    startLine.Drawing(g, m);
                }
                if (this.endLine != null)
                {
                    endLine.Drawing(g, m);
                }

                if (this.simpleLineList.Count > 0)
                {
                    for (int i = 0; i < simpleLineList.Count; i++)
                    {
                        SimpleLine sLine = simpleLineList[i];
                        int mid = simpleLineList.Count / 2;
                        if (i == mid)
                        {
                            //画name
                            g.DrawString(LineName, drawFont, drawBrush,
                                new Point((sLine.GetStartShowPoint().X + sLine.GetEndShowPoint().X) / 2 - LineName.Length * 3, (sLine.GetStartShowPoint().Y + sLine.GetEndShowPoint().Y) / 2));
                        }
                        sLine.Drawing(g, m);
                    }
                }
                else
                {
                    //画name
                    g.DrawString(LineName, drawFont, drawBrush,
                        new Point((startLine.GetStartShowPoint().X + startLine.GetEndShowPoint().X) / 2 - LineName.Length * 3, (startLine.GetStartShowPoint().Y + startLine.GetEndShowPoint().Y) / 2));
                }
            }
        }


        public OriginalLine GetSelectedPart(Point mousePoint)
        {
            OriginalLine partLine = null;
            if (this.isComplex)
            {


                if (startLine.IsSelectedLine(mousePoint))
                {
                    partLine = startLine;
                }

                if (this.endLine.IsSelectedLine(mousePoint))
                {
                    partLine = this.endLine;
                }

                if (this.simpleLineList.Count > 0)
                {
                    foreach (SimpleLine sline in this.simpleLineList)
                    {
                        if (sline.IsSelectedLine(mousePoint))
                        {
                            partLine = sline;
                            break;
                        }
                    }
                }
            }
            else
            {
                if (base.IsSelectedLine(mousePoint))
                {
                    partLine = this;
                }
            }
            return partLine;


        }

        new public bool IsSelectedLine(Point mousePoint)
        {
            if (this.isComplex)
            {
                return this.IsSelectedComplexLine(mousePoint);
            }
            else
            {
                return IsSelectedLine(mousePoint, this.GetStartShowPoint(), this.GetEndShowPoint());
            }
        }

        public bool IsSelectedComplexLine(Point mousePoint)
        {
            OriginalLine selectedComplexLine = this.GetSelectedPart(mousePoint);
            if (selectedComplexLine != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void AddTurningPoint(Point mouseDownPoint, Point mouseUpPoint)
        {
            if (!isComplex)
            {
                this.startLine = new StartLine(this.StartPoint, mouseUpPoint, this.StartDiagram, this.id);
                this.endLine = new EndLine(mouseUpPoint, this.EndPoint, this.EndDiagram, this.id);
                isComplex = true;
            }
            else
            {
                this.selectedPart = this.GetSelectedPart(mouseDownPoint);
                if (selectedPart is StartLine)
                {
                    StartLine newStartLine = new StartLine(this.StartPoint, mouseUpPoint, this.StartDiagram, this.id);
                    SimpleLine newPart = new SimpleLine(mouseUpPoint, this.startLine.EndPoint, this.id);
                    this.simpleLineList.Add(newPart);
                    this.startLine = newStartLine;
                }
                else if (selectedPart is EndLine)
                {
                    EndLine newEndLine = new EndLine(mouseUpPoint, this.EndPoint, this.EndDiagram, this.id);
                    SimpleLine newPart = new SimpleLine(this.endLine.StartPoint, mouseUpPoint, this.id);
                    this.simpleLineList.Add(newPart);
                    this.endLine = newEndLine;
                }
                else if (selectedPart is SimpleLine)
                {
                    SimpleLine newPart1 = new SimpleLine(selectedPart.StartPoint, mouseUpPoint, this.id);
                    SimpleLine newPart2 = new SimpleLine(mouseUpPoint, selectedPart.EndPoint, this.id);
                    this.simpleLineList.Remove((SimpleLine)selectedPart);
                    this.simpleLineList.Add(newPart1);
                    this.simpleLineList.Add(newPart2);
                }

            }



        }
        new public Point GetStartShowPoint()
        {
            if (isComplex)
            {
                return this.startLine.GetStartShowPoint();
            }
            else
            {
                return base.GetStartShowPoint();
            }

        }

        new public Point GetEndShowPoint()
        {
            if (isComplex)
            {
                return this.endLine.GetEndShowPoint();
            }
            else
            {
                return base.GetEndShowPoint();
            }
        }

        public void ChangeX(int xChange)
        {
            if (isComplex)
            {
            
            this.startLine.StartPoint = new Point(startLine.StartPoint.X + xChange, startLine.StartPoint.Y);
            this.startLine.EndPoint = new Point(startLine.EndPoint.X + xChange, startLine.EndPoint.Y);

            this.endLine.StartPoint = new Point(endLine.StartPoint.X + xChange, endLine.StartPoint.Y);
            this.endLine.EndPoint = new Point(endLine.EndPoint.X + xChange, endLine.EndPoint.Y);


            if (this.simpleLineList.Count>0)
            {
                foreach (SimpleLine sline in simpleLineList)
                {
                    sline.StartPoint = new Point(sline.StartPoint.X + xChange, sline.StartPoint.Y);
                    sline.EndPoint = new Point(sline.EndPoint.X + xChange, sline.EndPoint.Y);

                }
            }
            }
            else
            {
                this.StartPoint = new Point(StartPoint.X + xChange, StartPoint.Y);
                this.EndPoint = new Point(EndPoint.X + xChange, EndPoint.Y);
            }
            
        }


        public void ChangeY(int yChange)
        {
            if (isComplex)
            {
            
            this.startLine.StartPoint = new Point(startLine.StartPoint.X, startLine.StartPoint.Y + yChange);
            this.startLine.EndPoint = new Point(startLine.EndPoint.X, startLine.EndPoint.Y + yChange);

            this.endLine.StartPoint = new Point(endLine.StartPoint.X, endLine.StartPoint.Y + yChange);
            this.endLine.EndPoint = new Point(endLine.EndPoint.X, endLine.EndPoint.Y + yChange);

            if (this.simpleLineList.Count > 0)
            {
                foreach (SimpleLine sline in simpleLineList)
                {
                    sline.StartPoint = new Point(sline.StartPoint.X, sline.StartPoint.Y + yChange);
                    sline.EndPoint = new Point(sline.EndPoint.X, sline.EndPoint.Y + yChange);

                }
            }
            }
            else
            {
                this.StartPoint = new Point(this.StartPoint.X, this.StartPoint.Y + yChange);
                this.EndPoint = new Point(this.EndPoint.X, this.EndPoint.Y + yChange);
            }
        }
    }
}
